package webExperiment;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;

public class PieChart extends Canvas {
    /**
	 * 
	 */
    private static final long serialVersionUID = 1L;
    protected Color Foreground;
    protected Color Background;
    protected int angle;
    private double proz;
    private int w, h;

    public PieChart(int value, Color ForegroundColor, Color BackgroundColor) {
	Foreground = ForegroundColor;
	Background = BackgroundColor;
	setValue(value);
    }

    public PieChart() {
	Foreground = new Color(163, 184, 204);
	Background = null;
	setValue(0);
    }

    public void setValue(int value) {
	proz = value / (double) 100;
	angle = Math.max(2, (int) (360 * proz + 0.5));
	while (angle > 360) {
	    angle = angle - 360;
	}
	angle = -angle;
	w = getSize().width - 1;
	h = getSize().height - 1;
	this.repaint();
    }

    @Override
    public void paint(Graphics g) {
	if (Background != null) {
	    g.setColor(Background);
	    g.fillArc(0, 0, w, h, 0, 360);
	}
	if (proz > 0) {
	    g.setColor(Foreground);
	    g.fillArc(0, 0, w, h, 90, angle);
	}
	g.setColor(Color.GRAY);
	g.drawOval(0, 0, w - 1, h - 1);
    }

}
